﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using MM.Common.UI.Controls;
using System.ComponentModel;
using DevExpress.XtraTreeList.Columns;
using DevExpress.XtraTreeList;
using MM.Common.Base;
using MM.Common.DBBase;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmEmp_Reg : XtraForm
    {
        public int EmpID;
        public FrmEmp_Reg()
        {
            InitializeComponent();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.No;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (SystemInfo.DbData.IsOpen)
            {
                SystemInfo.DbData.ExecuteSql(string.Format("delete From MXHLY_SYS_EMP_RGN where EMP_ID = {0}", this.EmpID));
                foreach (ListItemOrganization organization in this.ListREGIONAL.Items)
                {
                    DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("EMPID", this.EmpID), new InputDataParam("ORGCODE", organization.ORG_CODE) });
                    SystemInfo.DbData.ExecuteSql("insert into MXHLY_SYS_EMP_RGN values(:EMPID,:ORGCODE)", parms);
                }
                base.Hide();
            }
        }
        public void Edit(int id)
        {
            this.EmpID = id;
            this.getTreeData();
            this.getSelectedReg();
            base.ShowDialog();
        }

        private void FrmOrg_Reg_Load(object sender, EventArgs e)
        {
        }

        private void getSelectedReg()
        {
            DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("EMPID", this.EmpID) });
            string commandtext = "select KTSYS_ORGANIZATION.ORG_CODE,KTSYS_ORGANIZATION.ORG_NAME from MXHLY_SYS_EMP_RGN\r\n                            left join KTSYS_ORGANIZATION on MXHLY_SYS_EMP_RGN.ORG_CODE=KTSYS_ORGANIZATION.ORG_CODE where MXHLY_SYS_EMP_RGN.EMP_ID=:EMPID";
            DataTable dt = new DataTable();
            if (SystemInfo.DbData.Retrieve(commandtext, parm, ref dt))
            {
                this.ListREGIONAL.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListItemOrganization item = new ListItemOrganization
                    {
                        ORG_CODE = dt.Rows[i]["ORG_CODE"].ToString(),
                        ORG_NAME = dt.Rows[i]["ORG_NAME"].ToString()
                    };
                    this.ListREGIONAL.Items.Add(item);
                }
            }
        }

        private void getTreeData()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("SELECT T1.* FROM KTSYS_ORGANIZATION T1");
            builder.AppendLine("    INNER JOIN KTSYS_ORGANIZATION T2 ON T2.ORG_CODE = '" + SystemInfo.OrgHeadCode + "' ");
            builder.AppendLine("    WHERE T1.ORG_CODE = T2.ORG_CODE OR T1.ORG_PARENT_ID = T2.ORG_ID");
            DataTable dt = new DataTable();
            if (SystemInfo.DbData.Retrieve(builder.ToString(), ref dt))
            {
                this.treeREGIONAL.DataSource = dt;
            }
        }
        private bool IsExistOrganization()
        {
            foreach (ListItemOrganization organization in this.ListREGIONAL.Items)
            {
                if (organization.ORG_CODE == this.treeREGIONAL.FocusedColumn.FieldName)
                {
                    return true;
                }
            }
            return false;
        }

        private void toDelete_Click(object sender, EventArgs e)
        {
            for (int i = this.ListREGIONAL.SelectedItems.Count; i > 0; i--)
            {
                this.ListREGIONAL.Items.Remove(this.ListREGIONAL.SelectedItems[i - 1]);
            }
        }

        private void toSelect_Click(object sender, EventArgs e)
        {
            if (!this.IsExistOrganization())
            {
                ListItemOrganization item = new ListItemOrganization
                {
                    ORG_CODE = this.treeREGIONAL.FocusedNode.GetValue("ORG_CODE").ToString(),
                    ORG_NAME = this.treeREGIONAL.FocusedNode.GetValue("ORG_NAME").ToString()
                };
                this.ListREGIONAL.Items.Add(item);
            }
        }

        // Nested Types
        private class ListItemOrganization
        {
            // Fields
            private string _ORG_CODE;
            private string _ORG_NAME;

            // Methods
            public ListItemOrganization()
            {
            }

            public ListItemOrganization(string ORG_CODE, string ORG_NAME)
            {
                this._ORG_CODE = ORG_CODE;
                this._ORG_NAME = ORG_NAME;
            }

            public override string ToString()
            {
                return this._ORG_NAME;
            }

            // Properties
            public string ORG_CODE
            {
                get
                {
                    return this._ORG_CODE;
                }
                set
                {
                    this._ORG_CODE = value;
                }
            }

            public string ORG_NAME
            {
                get
                {
                    return this._ORG_NAME;
                }
                set
                {
                    this._ORG_NAME = value;
                }
            }
        }
    }
}
